package com.yanfan.data.controller;

import com.yanfan.common.annotation.Anonymous;
import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.iot.domain.AlertLog;
import com.yanfan.iot.service.IAlertLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.yanfan.common.utils.SecurityUtils.getLoginUser;

/**
 * 设备告警Controller
 *
 * @author kerwincui
 * @date 2022-01-13
 */
@Api(tags = "设备告警alertLog模块")
@RestController
@RequestMapping("/iot/alertLog")
public class AlertLogController extends BaseController {
    @Autowired
    private IAlertLogService alertLogService;

    /**
     * 查询设备告警列表
     */
    @ApiOperation("查询设备告警列表")
    //@PreAuthorize("@ss.hasPermi('iot:alert:list')")
    @GetMapping("/list")
    public TableDataInfo list(AlertLog alertLog) {
        startPage();
        if (!SecurityUtils.isAdmin()) {
            SysUser user = getLoginUser().getUser();
            // 查询所属机构
            if (null != user.getDeptId()) {
                alertLog.setUserId(user.getDept().getDeptUserId());
            } else {
                alertLog.setUserId(user.getUserId());
            }
        }
        List<AlertLog> list = alertLogService.selectAlertLogList(alertLog);
        return getDataTable(list);
    }

    @ApiOperation("查询设备告警列表")
    @GetMapping("/listecharts")
    public AjaxResult listecharts(AlertLog alertLog) {
        AjaxResult ajaxResult = new AjaxResult();
        List<AlertLog> list = alertLogService.selectAlertLogList(alertLog);
        ajaxResult.put("data", list);
        return ajaxResult;
    }

    /**
     * 导出设备告警列表
     */
    @ApiOperation("导出设备告警列表")
    //@PreAuthorize("@ss.hasPermi('iot:alert:export')")
    @Log(title = "设备告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlertLog alertLog) {
        List<AlertLog> list = alertLogService.selectAlertLogList(alertLog);
        ExcelUtil<AlertLog> util = new ExcelUtil<AlertLog>(AlertLog.class);
        util.exportExcel(response, list, "设备告警数据");
    }

    /**
     * 获取设备告警详细信息
     */
    @ApiOperation("获取设备告警详细信息")
    //@PreAuthorize("@ss.hasPermi('iot:alert:query')")
    @GetMapping(value = "/{alertLogId}")
    public AjaxResult getInfo(@PathVariable("alertLogId") Long alertLogId) {
        return AjaxResult.success(alertLogService.selectAlertLogByAlertLogId(alertLogId));
    }

    /**
     * 新增设备告警
     */
    @ApiOperation("新增设备告警")
    //@PreAuthorize("@ss.hasPermi('iot:alert:add')")
    @Log(title = "设备告警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AlertLog alertLog) {
        return toAjax(alertLogService.insertAlertLog(alertLog));
    }

    /**
     * 修改设备告警
     */
    @ApiOperation("修改设备告警")
    //@PreAuthorize("@ss.hasPermi('iot:alert:edit')")
    @Log(title = "设备告警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AlertLog alertLog) {
        return toAjax(alertLogService.updateAlertLog(alertLog));
    }

    /**
     * 修改设备告警
     */
    @ApiOperation("修改设备告警")
    //@PreAuthorize("@ss.hasPermi('iot:alert:remove')")
    @Log(title = "设备告警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{alertLogIds}")
    public AjaxResult remove(@PathVariable Long[] alertLogIds) {
        return toAjax(alertLogService.deleteAlertLogByAlertLogIds(alertLogIds));
    }

    @ApiOperation("查询设备告警列表")
    @GetMapping("/iotlistecharts")
    @Anonymous
    public AjaxResult iotlistecharts(AlertLog alertLog) {
        return alertLogService.selectAlertLogLists(alertLog);
    }

    @ApiOperation("统计设备告警等级")
    @GetMapping("/listAlertLogEchartsByType")
    public AjaxResult listAlertLogEchartsByType(AlertLog alertLog) {
        return alertLogService.listAlertLogEchartsByType(alertLog);
    }

    @ApiOperation("统计设备告警处理情况")
    @GetMapping("/listDeviceLogCount")
    public AjaxResult listDeviceLogCount(AlertLog alertLog) {
        return alertLogService.listDeviceLogCount(alertLog);
    }

    @ApiOperation("统计前五个产品的告警数量")
    @GetMapping("/iotProductCount")
    public AjaxResult getIotProductCount(){
        return alertLogService.getIotProductCount();
    }
}
